package com.baobao.web;

import com.baobao.dao.NoticeDAO;

import com.baobao.domain.Notice;

import com.baobao.service.NoticeService;

import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.servlet.ModelAndView;

/**
 * Spring MVC controller that handles CRUD requests for Notice entities
 * 
 */

@Controller("NoticeController")
public class NoticeController {

	/**
	 * DAO injected by Spring that manages Notice entities
	 * 
	 */
	@Autowired
	private NoticeDAO noticeDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Notice entities
	 * 
	 */
	@Autowired
	private NoticeService noticeService;

	/**
	 * Save an existing Notice entity
	 * 
	 */
	@RequestMapping("/saveNotice")
	public String saveNotice(@ModelAttribute Notice notice) {
		noticeService.saveNotice(notice);
		return "forward:/indexNotice";
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Edit an existing Notice entity
	 * 
	 */
	@RequestMapping("/editNotice")
	public ModelAndView editNotice(@RequestParam String schoolIdKey, @RequestParam Calendar timeKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("notice", noticeDAO.findNoticeByPrimaryKey(schoolIdKey, timeKey));
		mav.setViewName("notice/editNotice.jsp");

		return mav;
	}

	/**
	 * Create a new Notice entity
	 * 
	 */
	@RequestMapping("/newNotice")
	public ModelAndView newNotice() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("notice", new Notice());
		mav.addObject("newFlag", true);
		mav.setViewName("notice/editNotice.jsp");

		return mav;
	}

	/**
	 * Delete an existing Notice entity
	 * 
	 */
	@RequestMapping("/deleteNotice")
	public String deleteNotice(@RequestParam String schoolIdKey, @RequestParam Calendar timeKey) {
		Notice notice = noticeDAO.findNoticeByPrimaryKey(schoolIdKey, timeKey);
		noticeService.deleteNotice(notice);
		return "forward:/indexNotice";
	}

	/**
	 */
	@RequestMapping("/noticeController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request, @ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

	/**
	 * Select an existing Notice entity
	 * 
	 */
	@RequestMapping("/selectNotice")
	public ModelAndView selectNotice(@RequestParam String schoolIdKey, @RequestParam Calendar timeKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("notice", noticeDAO.findNoticeByPrimaryKey(schoolIdKey, timeKey));
		mav.setViewName("notice/viewNotice.jsp");

		return mav;
	}

	/**
	 * Entry point to show all Notice entities
	 * 
	 */
	public String indexNotice() {
		return "redirect:/indexNotice";
	}

	/**
	 * Select the Notice entity for display allowing the user to confirm that they would like to delete the entity
	 * 
	 */
	@RequestMapping("/confirmDeleteNotice")
	public ModelAndView confirmDeleteNotice(@RequestParam String schoolIdKey, @RequestParam Calendar timeKey) {
		ModelAndView mav = new ModelAndView();

		mav.addObject("notice", noticeDAO.findNoticeByPrimaryKey(schoolIdKey, timeKey));
		mav.setViewName("notice/deleteNotice.jsp");

		return mav;
	}

	/**
	 * Show all Notice entities
	 * 
	 */
	@RequestMapping("/indexNotice")
	public ModelAndView listNotices() {
		ModelAndView mav = new ModelAndView();

		mav.addObject("notices", noticeService.loadNotices());

		mav.setViewName("notice/listNotices.jsp");

		return mav;
	}
}